package com.ruoyi.odorservice.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.odorservice.entity.Standard;
import com.ruoyi.odorservice.entity.StandardFiles;
import com.ruoyi.odorservice.entity.vo.StandardFilesRequestBean;
import com.ruoyi.odorservice.entity.vo.StandardRequestBean;
import com.ruoyi.odorservice.service.MinIOService;
import com.ruoyi.odorservice.service.StandardFilesService;
import com.ruoyi.odorservice.service.StandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/standard")
@Api(tags = "政策文件")
@AllArgsConstructor
@Slf4j
public class StandardController extends BaseController {

    private StandardFilesService standardFilesService;
    private StandardService standardService;
    private MinIOService minIOService;
    @GetMapping("/pages")
    @ApiOperation(value = "分页查询(查询标准及限制)",httpMethod = "GET", response = StandardFiles.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer" , required = false),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer" , required = false),
            @ApiImplicitParam(name = "standardName", value = "标准名称", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "standardNo", value = "标准编号", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "standardType", value = "标准类型", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "materialName", value = "物质名称", paramType = "query", dataType = "String", required = false)
    })
    public TableDataInfo pages(@RequestParam(value ="page", defaultValue = "1", required = false) Integer page,
                               @RequestParam(value ="limit", defaultValue = "10", required = false) Integer limit,
                               @RequestParam(value = "standardName", required = false) String standardName,
                               @RequestParam(value = "standardNo", required = false) String standardNo,
                               @RequestParam(value = "standardType", required = false) String standardType,
                               @RequestParam(value = "materialName", required = false) String materialName) {
        startPage();
        List<Standard> list = standardService.list(new LambdaQueryWrapper<Standard>()
                .like(StrUtil.isNotEmpty(standardNo),Standard::getStandardNo,standardNo)
                .like(StrUtil.isNotEmpty(standardName),Standard::getStandardName,standardName)
                .eq(StrUtil.isNotEmpty(standardType),Standard::getStandardType,standardType)
                .like(StrUtil.isNotEmpty(materialName),Standard::getMaterialName,materialName)
        );
        return getDataTable(list);
    }
    @GetMapping("/standardPages")
    @ApiOperation(value = "分页查询标准政策文件信息", httpMethod = "GET", response = Standard.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", required = false),
        @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", required = false),
        @ApiImplicitParam(name = "standardName", value = "标准名称", paramType = "query", dataType = "String", required = false),
        @ApiImplicitParam(name = "standardNo", value = "标准编号", paramType = "query", dataType = "String", required = false),
        @ApiImplicitParam(name = "standardType", value = "标准类型", paramType = "query", dataType = "String", required = false)
    })
    public TableDataInfo standardPages(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "standardName", required = false) String standardName,
            @RequestParam(value = "standardNo", required = false) String standardNo,
            @RequestParam(value = "standardType", required = false) String standardType) {
        startPage();
        List<StandardFiles> list = standardFilesService.list(new LambdaQueryWrapper<StandardFiles>()
                .like(StrUtil.isNotEmpty(standardNo),StandardFiles::getStandardNo,standardNo)
                .like(StrUtil.isNotEmpty(standardName),StandardFiles::getStandardName,standardName)
                .eq(StrUtil.isNotEmpty(standardType),StandardFiles::getStandardType,standardType)
        );
        return getDataTable(list);
    }

    @ApiOperation(value = "上传文件", httpMethod="POST" ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", paramType="form", value = "文件", dataType="file", required = true)
    })
    @PostMapping(value = "/upload")
    @ResponseBody
    public Result upload(@RequestPart @RequestParam("file") MultipartFile file){
        try {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            String objectName = IdUtil.simpleUUID() + "." + suffix;
            String path = minIOService.putObject("odor-standard-files", objectName, file.getInputStream());
            if (StrUtil.isNotBlank(path)){
                return Result.ok(path);
            }else {
                return Result.fail("上传失败！");
            }
        } catch (Exception e) {
            throw new BaseException("操作错误！");
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加政策文件", httpMethod = "POST")
    public Result<Boolean> addCompany(@RequestBody StandardFilesRequestBean requestBean) {
        StandardFiles standardFiles = new StandardFiles();
        BeanUtils.copyProperties(requestBean,standardFiles);
        standardFiles.setCreateTime(LocalDateTime.now());
        standardFilesService.save(standardFiles);
        return Result.ok();
    }

    @PostMapping("/updateStandardFiles")
    @ApiOperation(value = "更新标准文件信息", httpMethod = "POST")
    public Result<Boolean> updateStandardFiles(@RequestBody StandardFilesRequestBean requestBean) {
        StandardFiles standardFiles = new StandardFiles();
        BeanUtils.copyProperties(requestBean, standardFiles);
        if(requestBean.getId() == null){
            throw new BaseException("id不能为空！");
        }
        standardFiles.setUpdateTime(LocalDateTime.now());
        boolean updated = standardFilesService.updateById(standardFiles);
        if (updated) {
            if (StrUtil.isNotBlank(requestBean.getFileUrl())){
                LambdaUpdateWrapper<Standard> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Standard::getStandardName, standardFiles.getStandardName())
                        .set(Standard::getFileUrl, standardFiles.getFileUrl())
                        .set(Standard::getUpdateTime, LocalDateTime.now());
                standardService.update(updateWrapper);
            }
            return Result.ok();
        } else {
            return Result.fail("更新失败！");
        }
    }

    @PostMapping("/updateStandard")
    @ApiOperation(value = "更新标准及限制信息", httpMethod = "POST")
    public Result<Boolean> updateStandard(@RequestBody StandardRequestBean requestBean) {
        Standard standard = new Standard();
        BeanUtils.copyProperties(requestBean, standard);
        if(requestBean.getId() == null){
            throw new BaseException("id不能为空！");
        }
        standard.setUpdateTime(LocalDateTime.now());
        boolean updated = standardService.updateById(standard);
        if (updated) {
            return Result.ok();
        } else {
            return Result.fail("更新失败！");
        }
    }

}
